package com.example.demo.controller;

import com.example.demo.entity.GoodsItem;
import com.example.demo.service.GoodServer;
import com.sun.istack.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("goods")
public class goodsController {
    @Autowired
    GoodServer goodServer;
    //get:localhost:8080/goods/1
    @ApiOperation("根据id查询商品")
    @GetMapping("/{id}")
    public ResponseEntity<GoodsItem> getGoods(@ApiParam("商品id")@PathVariable long id){
        GoodsItem result = goodServer.getGoods(id);
        if(result == null){
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    //get:localhost:8080/goods
    //get:localhost:8080/goods?name=哇哈哈
    //get:localhost:8080/goods?name=哇哈哈&&price=8.0
   /* @ApiOperation("根据条件查询商品")
    @GetMapping("")
    public ResponseEntity<List<GoodsItem>> findGoodss(@ApiParam("商品名称")String name,@ApiParam("商品价格") Double price){
        List<GoodsItem> result = goodServer.findGoodses(name,price);
        return ResponseEntity.ok(result);
    }*/
    @ApiOperation("根据条件查询商品")
    @GetMapping("")
    public Page<GoodsItem> findGoodses(String name,
                                       Double price,
                                       String supplierName,
                                       @RequestParam(defaultValue = "0") Integer pageNum,
                                       @RequestParam(defaultValue = "10")Integer pageSize){
        Map<String,Object> condition = new HashMap<>();
        if(name != null){
            condition.put("name","%"+name+"%");
        }
        if(price!=null) {
            condition.put("price",price);
        }
        if(supplierName!=null) {
            condition.put("supplierName",supplierName);
        }
        return goodServer.findGoods(condition, PageRequest.of(pageNum,pageSize, Sort.by("id")));
    }

    //post:localhost:8080/goods
    @ApiOperation("添加商品")
    @PostMapping("")
    public ResponseEntity<GoodsItem> addGoods(@RequestBody GoodsItem goods){
        GoodsItem result = goodServer.addGoods(goods);
        return ResponseEntity.ok(result);
    }

    //put:localhost:8080/goods/1
    @ApiOperation("修改商品")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateGoods(@PathVariable long id,@RequestBody GoodsItem goods){
        goodServer.updateGoods(id,goods);
        return ResponseEntity.ok().build();
    }

    //delete:localhost:8080/goods/1
    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable long id){
        goodServer.deleteGoods(id);
        return ResponseEntity.ok().build();
    }

}
